{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Hexspeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: toHexspeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #十六进制魔术数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你有一个十进制数字，请按照此规则将它变成「十六进制魔术数字」：首先将它变成字母大写的十六进制字符串，然后将所有的数字&nbsp;<code>0</code> 变成字母&nbsp;<code>O</code> ，将数字&nbsp;<code>1</code> &nbsp;变成字母&nbsp;<code>I</code> 。</p>\n",
    "\n",
    "<p>如果一个数字在转换后只包含&nbsp;<code>{&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;I&quot;, &quot;O&quot;}</code>&nbsp;，那么我们就认为这个转换是有效的。</p>\n",
    "\n",
    "<p>给你一个字符串&nbsp;<code>num</code> ，它表示一个十进制数 <code>N</code>，如果它的十六进制魔术数字转换是有效的，请返回转换后的结果，否则返回&nbsp;<code>&quot;ERROR&quot;</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = &quot;257&quot;\n",
    "<strong>输出：</strong>&quot;IOI&quot;\n",
    "<strong>解释：</strong>257 的十六进制表示是 101 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = &quot;3&quot;\n",
    "<strong>输出：</strong>&quot;ERROR&quot;\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= N &lt;= 10^12</code></li>\n",
    "\t<li>给定字符串不会有前导 0 。</li>\n",
    "\t<li>结果中的所有字母都应该是大写字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [hexspeak](https://leetcode.cn/problems/hexspeak/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [hexspeak](https://leetcode.cn/problems/hexspeak/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"257\"', '\"3\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        n = int(num)\n",
    "        dct = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"I\", \"O\"]\n",
    "        res = []\n",
    "        while n:\n",
    "            m = n % 16\n",
    "            if not (m <= 1 or m >= 10):\n",
    "                return \"ERROR\"\n",
    "            res.append(m)\n",
    "            n //= 16\n",
    "        res.reverse()\n",
    "        ans = \"\"\n",
    "        for w in res:\n",
    "            if w >= 10:\n",
    "                ans += dct[w-10]\n",
    "            else:\n",
    "                ans += \"OI\"[w]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num_hex = hex(int(num))[2:]\n",
    "        transform = {\n",
    "            \"0\": \"O\",\n",
    "            \"1\": \"I\",\n",
    "            \"a\": \"A\",\n",
    "            \"b\": \"B\",\n",
    "            \"c\": \"C\",\n",
    "            \"d\": \"D\",\n",
    "            \"e\": \"E\",\n",
    "            \"f\": \"F\",\n",
    "        }\n",
    "        ans=\"\"\n",
    "        for ch in num_hex:\n",
    "            if ch not in transform:\n",
    "                return \"ERROR\"\n",
    "            ans+=transform[ch]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num_hex = hex(int(num))[2:]\n",
    "        transform = {\n",
    "            \"0\" : \"O\",\n",
    "            \"1\" : \"I\",\n",
    "            \"a\": \"A\",\n",
    "            \"b\": \"B\",\n",
    "            \"c\": \"C\",\n",
    "            \"d\": \"D\",\n",
    "            \"e\": \"E\",\n",
    "            \"f\": \"F\",\n",
    "        }\n",
    "        ans = \"\"\n",
    "        for ch in num_hex:\n",
    "            if ch not in transform:\n",
    "                return \"ERROR\"\n",
    "\n",
    "            ans += transform[ch]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num_hex = hex(int(num))[2:]\n",
    "        transform = {\n",
    "            \"0\": \"O\",\n",
    "            \"1\": \"I\",\n",
    "            \"a\": \"A\",\n",
    "            \"b\": \"B\",\n",
    "            \"c\": \"C\",\n",
    "            \"d\": \"D\",\n",
    "            \"e\": \"E\",\n",
    "            \"f\": \"F\",\n",
    "        }\n",
    "        \n",
    "        ans = \"\"\n",
    "        for ch in num_hex:\n",
    "            if ch not in transform:\n",
    "                return \"ERROR\"\n",
    "            ans += transform[ch]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def toHexspeak(self, num: str) -> str:\n",
    "#         cmp=\"ABCDEFIO\"\n",
    "#         num=list(hex(int(num))[2:])\n",
    "#         for i,ch in enumerate(num):\n",
    "#             if ch.isalpha():\n",
    "#                 num[i]=ch.upper()\n",
    "#             elif ch==\"0\":\n",
    "#                 num[i]=\"O\"\n",
    "#             elif ch==\"1\":\n",
    "#                 num[i]=\"I\"\n",
    "#             else:\n",
    "#                 return \"ERROR\"\n",
    "#         return \"\".join(num)\n",
    "            \n",
    "### 官解：字典\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num_hex = hex(int(num))[2:]\n",
    "        transform = {\n",
    "            \"0\": \"O\",\n",
    "            \"1\": \"I\",\n",
    "            \"a\": \"A\",\n",
    "            \"b\": \"B\",\n",
    "            \"c\": \"C\",\n",
    "            \"d\": \"D\",\n",
    "            \"e\": \"E\",\n",
    "            \"f\": \"F\",\n",
    "        }\n",
    "        \n",
    "        ans = \"\"\n",
    "        for ch in num_hex:\n",
    "            if ch not in transform:\n",
    "                return \"ERROR\"\n",
    "            ans += transform[ch]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        s = hex(int(num))[2:]\n",
    "        dic = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', '0': 'O', '1': 'I'}\n",
    "        ans = ''\n",
    "        for x in s:\n",
    "            if x not in dic:\n",
    "                return \"ERROR\"\n",
    "            ans += dic[x]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        hexstr = hex(int(num))[2:].upper()\n",
    "        for i in range(len(hexstr)):\n",
    "            if hexstr[i] == '0':\n",
    "                hexstr = hexstr[:i] + 'O' + hexstr[i+1:]\n",
    "            if hexstr[i] == '1':\n",
    "                hexstr = hexstr[:i] + 'I' + hexstr[i+1:]\n",
    "            if hexstr[i] not in ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'O']:\n",
    "                return \"ERROR\"\n",
    "        return hexstr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num_hex = hex(int(num))[2:]\n",
    "        transform = {\n",
    "            \"0\": \"O\",\n",
    "            \"1\": \"I\",\n",
    "            \"a\": \"A\",\n",
    "            \"b\": \"B\",\n",
    "            \"c\": \"C\",\n",
    "            \"d\": \"D\",\n",
    "            \"e\": \"E\",\n",
    "            \"f\": \"F\",\n",
    "        }\n",
    "        \n",
    "        ans = \"\"\n",
    "        for ch in num_hex:\n",
    "            if ch not in transform:\n",
    "                return \"ERROR\"\n",
    "            ans += transform[ch]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        hex_num=hex(int(num))[2:]\n",
    "        translist={'0':'O','1':'I','a':'A','b':'B','c':'C','d':'D','e':'E','f':'F'}\n",
    "        ans=''\n",
    "        for i in hex_num:\n",
    "            if i in translist:\n",
    "                ans+=translist[i]\n",
    "            else:\n",
    "                return \"ERROR\"\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        cmp=\"ABCDEFIO\"\n",
    "        num=list(hex(int(num))[2:])\n",
    "        for i,ch in enumerate(num):\n",
    "            if ch.isalpha():\n",
    "                num[i]=ch.upper()\n",
    "            elif ch==\"0\":\n",
    "                num[i]=\"O\"\n",
    "            elif ch==\"1\":\n",
    "                num[i]=\"I\"\n",
    "            else:\n",
    "                return \"ERROR\"\n",
    "        return \"\".join(num)\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        hexnum = hex(int(num))\n",
    "        hexnum = str(hexnum[2:])\n",
    "\n",
    "        hash = {'a':'A', 'b':'B', 'c':'C', 'd': 'D', 'e':'E', 'f':'F', '0':'O', '1':'I'}\n",
    "        res = ''\n",
    "        for i in hexnum:\n",
    "            if i in hash.keys():\n",
    "                res += hash[i]\n",
    "            else:\n",
    "                return 'ERROR'\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        a = list(hex(int(num)))[2:]\n",
    "        for i in range(len(a)):\n",
    "            if a[i] == '0':\n",
    "                a[i] = 'O'\n",
    "            elif a[i] == '1':\n",
    "                a[i] = 'I'\n",
    "            elif a[i] in ['2','3','4','5','6','7','8','9']:\n",
    "                return \"ERROR\"\n",
    "            else:\n",
    "                a[i] = chr(ord(a[i])+ord('A')-ord('a'))\n",
    "        return ''.join(a)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        transformDict = {\n",
    "            0: \"O\", \n",
    "            1: \"I\", \n",
    "            10: \"A\", \n",
    "            11: \"B\", \n",
    "            12: \"C\", \n",
    "            13: \"D\", \n",
    "            14: \"E\", \n",
    "            15: \"F\"\n",
    "        }\n",
    "        \n",
    "        ans = []\n",
    "\n",
    "        num = int(num)\n",
    "        while num:\n",
    "            remainder = num % 16\n",
    "            num //= 16\n",
    "            if remainder not in transformDict:\n",
    "                return \"ERROR\"\n",
    "            else:\n",
    "                ans.append(remainder)\n",
    "        return \"\".join([transformDict[x] for x in reversed(ans)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Python3 模拟\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        ref = hex(int(num))[2:].replace(\"0\",\"O\").replace(\"1\",\"I\").upper()\n",
    "        aset = {\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"I\",\"O\"}\n",
    "        for i in ref:\n",
    "            if i not in aset:\n",
    "                return \"ERROR\"\n",
    "        return ref\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHexspeak(self, num: str) -> str:\n",
    "        num = int(num)\n",
    "        ans = \"\"\n",
    "\n",
    "        key = \"OI________ABCDEF\"\n",
    "        while num:\n",
    "            ans = key[num % 16] + ans\n",
    "            num //= 16\n",
    "        \n",
    "        return ans if \"_\" not in ans else \"ERROR\""
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
